package com.lzit.smart_canteen.service;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.image.ImageClient;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
public class AiService {
    private final ChatClient chatClient;
    private final StreamingChatClient streamChatClient;

    public AiService(ChatClient chatClient, StreamingChatClient streamChatClient, ImageClient imageClient) {
        this.chatClient = chatClient;
        this.streamChatClient = streamChatClient;
        this.imageClient = imageClient;
    }

    public String normalCompletion(String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatClient.call(prompt).getResult().getOutput().getContent();
    }

    public Flux<ChatResponse> streamCompletion(String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.streamChatClient.stream(prompt);
    }

    private final ImageClient imageClient;

    /**
     * @param instructions 图片说明
     * @param options      图片规格：大小, 图片数量，图片输出形式byte[],base64等
     */
    public ImageResponse genImage(String instructions, ImageOptions options) {
        System.out.println("instructions:" + instructions);
        ImagePrompt request = new ImagePrompt(instructions, options);
        return this.imageClient.call(request);
    }
}
